<template>
    <transition name="modal">
        <div class="modal-mask" v-show="open">
            <div class="modal-wrapper">
                <div class="modal-container">
    
                    <div class="modal-header">
                        <div class="row">
                            <div class="col-lg-12">
                                <h4>编辑用户</h4>
                            </div>
                            
                        </div>
                       
                        
                        <button type="button" class="close" @click="$emit('close')">
                            <i class="fa fa-times" aria-hidden="true"></i>
                        </button>
                    </div>
    
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="form-horizontal">
                                    <div class="form-group">
                                        <label class="col-sm-4 control-label">电话：</label>
                                        <div class="col-sm-8">
                                            <p class="form-control-static">{{username}}</p>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-4 control-label">余额：</label>
                                        <div class="col-sm-8">
                                            <input type="number" class="form-control" min='0' v-model="balance">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-4 control-label">用户等级：</label>
                                        <div class="col-sm-8">
                                            <label class="radio-inline">
                                                <input type="radio" value="0" v-model="userLevel">客户</label>
                                            <label class="radio-inline">
                                                <input type="radio" value="1" v-model="userLevel">会员</label>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-4 control-label">地址：</label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" v-model="address" placeholder="请输入用户地址">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
    
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" @click="save"> 保 存 </button>
                        <button type="button" class="btn btn-default" @click="$emit('close')"> 取 消 </button>
                    </div>
                </div>
            </div>
        </div>
    </transition>
</template>
<script>
import {tip,ajax} from '../../tools/tools.js'
import {cfg} from '../../config/config.js'

export default {
    name: 'AddUserModal',
    data() {
        return {
            username:'',
            userLevel:'0',
            address:'',
            balance:0
        }
    },
    props:{
        open:{
            type:Boolean,
            default:false
        },
        dataObj:{
            type:Object,
            default:function(){
                return {}
            }
        }
    },
    computed:{
        
    },
    watch:{
        open(v,ov){
            if(v==false){
                this.clearData();
            }
        },
        dataObj:{
            deep:true,
            handler(v,ov){
                var self = this;
                self.username = v.phone;
                self.userLevel = v.fkLevel;
                self.balance = v.balance;
                self.address = v.address;
            }
        },
        balance(v){
            if(parseInt(v)>0){
                this.userLevel = '1';
            }
        }
    },
    methods: {
        clearData(){
            var self = this;
            self.username = '';
            self.userLevel = '0';
            self.balance = 0;
            self.address = '';
        },
        save(){
            var self = this;
            ajax.getUserInfo({
                type:'post',
                param:{
                    token:localStorage.token,
                    phone:self.username
                },
                cb(data){
                    if(data.rcode!=cfg.rcode.NOREQ){
                        ajax.editUserInfo({
                            type:'post',
                            param:{
                                username:self.username,
                                userLevel:self.userLevel,
                                address:self.address,
                                balance:self.balance,
                                token:localStorage.token
                            },
                            cb(data){
                                tip.info(`用户${self.username}编辑成功`)
                                self.$emit('editdone')
                            }
                        })
                          
                    } else {
                        tip.warn('用户不存在')  
                    }
                }
            })

        }
    },
    mounted() {
        
    }
}
</script>
<style scoped>
.close {
    position: absolute;
    right: 30px;
    top: 30px;
}

.modal-mask {
    position: fixed;
    z-index: 10001;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: rgba(0, 0, 0, .5);
    transition: opacity .3s ease;
}

.modal-wrapper {
    position: absolute;
    width: 100%;
    height: 1px;
    left: 0;
    right:0;
    top: 120px;
}

.limte-height{
    max-height: 250px;
    overflow-x: hidden;
    overflow-y: auto;
}

.modal-container {
    width: 800px;
    margin: 0px auto;
    padding: 20px 30px;
    background-color: #fff;
    border-radius: 2px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
    transition: all .3s ease;
    font-family: Helvetica, Arial, sans-serif;
    position: relative;
}

.modal-header h3 {
    margin-top: 0;
    color: #42b983;
}

.modal-default-button {
    float: right;
}

.modal-enter {
    opacity: 0;
}

.modal-leave-active {
    opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-active .modal-container {
    -webkit-transform: scale(1.1);
    transform: scale(1.1);
}
</style>
